﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PetGo
{

    class Sgf
    {
        public static string k_sgf_koordinaten = " abcdefghijklmnopqrst";
        public static string k_gtp_koordinaten = "abcdefghjklmnopqrstuvwxyz";
        public static string get_Sgf_Position(int themove, int boardsize)
        {
            int y = themove / boardsize + 1;
            int x = themove % (boardsize);
            return k_gtp_koordinaten.Substring(x, 1).ToUpper() + y.ToString();
        }
        public static string get_Sgf_Position_formated(int themove, int boardsize)
        {
            int y = themove / boardsize + 1;
            int x = themove % (boardsize);
            return k_gtp_koordinaten.Substring(x, 1).ToUpper() + y.ToString("00");
        }
        public static int get_Move_Position(string sgfPosition, int boardsize)
        {
            string letter = sgfPosition.Substring(0, 1).ToLower();
            sgfPosition = sgfPosition.Substring(1).Trim();
            string number = sgfPosition;
            int y = int.Parse(number);
            int x = Sgf.k_gtp_koordinaten.IndexOf(letter);
            return (boardsize) * (y - 1) + x;
        }
        public static int get_Move_Position_from_Sgf(string sgfPosition, int boardsize)
        {
            string letter = sgfPosition.Substring(0, 1).ToLower();
            sgfPosition = sgfPosition.Substring(1).Trim();
            int y = Sgf.k_sgf_koordinaten.IndexOf(sgfPosition);
            int x = Sgf.k_sgf_koordinaten.IndexOf(letter) - 1;
            return (boardsize) * (y - 1) + x;
        }
        //public static string getZug(int pos, farbe werdran, Spielfeld mySpielfeld)
        //{
        //    string theText = "";

        //    theText += ";" + werdran.ToString().Replace("schwarz", "B").Replace("weiss", "W") + "[" + getPos(pos, mySpielfeld) + "]";
        //    return theText;
        //}
        //public static string getCommentq(int pos, farbe werdran, double q, Spielfeld mySpielfeld)
        //{
        //    string theText = "";

        //    theText += "["+getPos(pos, mySpielfeld) + ":"+(q*100).ToString("00")+"]";
        //    return theText;
        //}
        //public static string getCommentqr(int pos, farbe werdran, double qr, Spielfeld mySpielfeld)
        //{
        //    string theText = "";

        //    theText += "[" + getPos(pos, mySpielfeld) + ":" + (qr * 100).ToString("00") + "]";
        //    return theText;
        //}
        //public static string getCommentvor(int pos, farbe werdran, int vorkommen, Spielfeld mySpielfeld)
        //{
        //    string theText = "";
        //    string theVor = vorkommen.ToString("000");
        //    if (vorkommen > 1000) theVor = (vorkommen / 1000).ToString() + "k" + ((vorkommen % 1000) / 100).ToString("0");

        //    theText += "[" + getPos(pos, mySpielfeld) + ":" + theVor + "]";
        //    return theText;
        //}

        //public static string getPos(int thepos, Spielfeld mySpielfeld)
        //{
        //    string theText = "";
        //    int posx = thepos % (mySpielfeld.myBrettLaenge + 2);
        //    int posy = thepos / (mySpielfeld.myBrettLaenge + 2);
        //    string x = k_sgf_koordinaten.Substring(posx, 1);
        //    string y = k_sgf_koordinaten.Substring(posy, 1);
        //    theText =  x + y;
        //    return theText;
        //}
        //public static void write(Bewertung theBewertung)
        //{
        //    string theText = "(";
        //    theText +=";FF[4]GM[1]AP[petgo]";
        //    theText +="PB[petgo]BR[99k?]";
        //    theText +="PW[petgo]BR[99k?]";
        //    theText += theBewertung.mykomi.ToString();
        //    theText += "SZ[" + theBewertung.mySpielfeld.myBrettLaenge + "]";
        //    //foreach (int pos in theBewertung.mySpielfeld.mySpielverlauf)
        //    //{
        //    //    theBewertung.mySpielfeld.mySGFbody += getZug(pos,werdran ,theBewertung.mySpielfeld);
        //    //    theBewertung.mySpielfeld.tausche();
        //    //    werdran = theBewertung.mySpielfeld.myfarbeamZug;
        //    //}
        //    theText += theBewertung.mySpielfeld.mySGFbody;
        //    //RE[W+12.5]
        //    //DT[1998-06-15]
        //    //TM[600]
        //    theText += ")";
        //    string theName = theBewertung.mySpielfeld.myBrettLaenge.ToString() + "x" +theBewertung.mySpielfeld.myBrettLaenge.ToString() + "_"
        //        + theBewertung.mysecondstothink.ToString()+"s_"
        //        + DateTime.Now.Year.ToString("0000")
        //        + DateTime.Now.Month.ToString("00")
        //        + DateTime.Now.Day.ToString("00")
        //        +"at"
        //        + DateTime.Now.Hour.ToString("00")
        //        + DateTime.Now.Minute.ToString("00")
        //        + DateTime.Now.Second.ToString("00")
        //        + "_petgo_petgo";
        //    string theDatei = "c:\\temp\\PetGo\\sgf\\" + theName + ".sgf";
        //    System.IO.File.WriteAllText(theDatei, theText);

        //}
        public static void writeLog(Tree theTree, Game theGame)
        {
            string theDatei = "c:\\temp\\PetGo\\sgf\\" + theGame.myName + ".log";
            try
            {
                System.IO.File.AppendAllText(theDatei, Game.chatstring(Tree.myRootBoard.myGame) + Environment.NewLine);

            }
            catch
            {
                theDatei = "c:\\xxx\\PetGo\\sgf\\" + theGame.myName + ".log";
                System.IO.File.AppendAllText(theDatei, Game.chatstring(Tree.myRootBoard.myGame) + Environment.NewLine);
            }
        }

        // reads an sgf + winner
        public static List<int> sgfHistorie(string filename, out color thewinner)
        {
            List<int> theReturn = new List<int>();
            string thesgfstring = System.IO.File.ReadAllText(filename);
            thewinner = color.black;
            if (thesgfstring.IndexOf("[W+") > 0)
            {
                thewinner = color.white;
            }
            string thesearch = ";B[";
            int start = thesgfstring.IndexOf(thesearch);
            while (start > 0)
            {
                string thepos = thesgfstring.Substring(start+3, 2);
                theReturn.Add(get_Move_Position_from_Sgf(thepos, 19));
                thesgfstring = thesgfstring.Substring(start+5);
                if (thesearch == ";B[") 
                {
                    thesearch = ";W[";
                }
                else
                {
                    thesearch = ";B[";
                }
                start = thesgfstring.IndexOf(thesearch);
            }

            return theReturn;
        }
        // find all sgfs, returns path and name
        public static List<string> getsgfs()
        {
            List<string> theReturn = new List<string>();
#if DEBUG
            DirSearch(@"C:\Users\peter\Documents\syn_user\Go\Progames\ChoChikun\test", theReturn);
            //DirSearch(thepath, theReturn);
            //DirSearch(@"C:\Users\peter\Documents\syn_user\Go\Progames", theReturn);
#else
            DirSearch(@"C:\Users\peter\Documents\syn_user\Go\Progames", theReturn);
#endif
            return theReturn;
        }
        // find all sgfs, returns path and name
        public static List<string> getsgfs(string thepath)
        {
            List<string> theReturn = new List<string>();
#if DEBUG
            //DirSearch(@"C:\Users\peter\Documents\syn_user\Go\Progames\ChoChikun\test", theReturn);
            DirSearch(thepath, theReturn);
            //DirSearch(@"C:\Users\peter\Documents\syn_user\Go\Progames", theReturn);
#else
            DirSearch(@"C:\Users\peter\Documents\syn_user\Go\Progames", theReturn);
#endif
            return theReturn;
        }
        public static void DirSearch(string sDir, List<string> theList)
        {
            try
            {
                foreach (string d in Directory.GetDirectories(sDir))
                {
                    foreach (string f in Directory.GetFiles(d, "*.sgf"))
                    {
                        theList.Add(f);
                    }
                    DirSearch(d, theList);
                }
            }
            catch (System.Exception excpt)
            {
                Console.WriteLine(excpt.Message);
            }
        }
    }
}
